/*
 * Copyright 2006 Abdulla G. Abdurakhmanov (abdulla.abdurakhmanov@gmail.com).
 * 
 * Licensed under the GPL, Version 2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.gnu.org/copyleft/gpl.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * With any your questions welcome to my e-mail 
 * or blog at http://abdulla-a.blogspot.com.
 */

package org.ofsm.creator;

import java.awt.Dimension;
import java.awt.Frame;

import java.awt.Rectangle;
import java.awt.Toolkit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;

import javax.swing.JLabel;
import javax.swing.JTextField;


public class ObjectEditorDialog extends JDialog {
    private JLabel lblName = new JLabel();
    private JLabel lblClassName = new JLabel();
    private JTextField edtClassName = new JTextField();
    private JTextField edtName = new JTextField();
    private JButton btnCancel = new JButton();
    private JButton btnOk = new JButton();

    public ObjectEditorDialog() {
        this(null, "", false);
    }

    public ObjectEditorDialog(Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        try {
            jbInit();
            jbInitEx();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        this.setModal(true);
        this.setSize(new Dimension(431, 136));
        lblName.setText("Name:");
        lblName.setBounds(new Rectangle(10, 10, 90, 15));
        lblClassName.setText("ClassName");
        lblClassName.setBounds(new Rectangle(10, 35, 80, 15));
        edtClassName.setBounds(new Rectangle(105, 35, 300, 20));
        btnOk.setText("Ok");
        btnOk.setEnabled(false);
        btnOk.setBounds(new Rectangle(120, 70, 80, 25));
        btnOk.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        btnOk_actionPerformed(e);
                    }
                });
        btnCancel.setText("Cancel");
        btnCancel.setBounds(new Rectangle(220, 70, 80, 25));
        btnCancel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        btnCancel_actionPerformed(e);
                    }
                });

        edtName.setBounds(new Rectangle(105, 10, 300, 20));
        edtName.addKeyListener(new KeyAdapter() {
                    public void keyReleased(KeyEvent e) {
                        edtName_keyReleased(e);
                    }
                });
        this.getContentPane().add(edtName,
                                  null);
        this.getContentPane().add(btnCancel,
                                  null);
        this.getContentPane().add(btnOk, null);
        this.getContentPane().add(edtClassName,
                                  null);
        this.getContentPane().add(lblClassName, 
                                  null);
        this.getContentPane().add(lblName, null);
    }

    private void jbInitEx() {
        //this.setSize(xYLayout.getWidth(),xYLayout.getHeight());
        this.setResizable(false);    
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
    }


    public String getName() {
        return edtName.getText();
    }
    
    public void setName(String name) {
        edtName.setText(name);
        if(name.length()>0)
            btnOk.setEnabled(true);
    }
    
    public String getClassName() {
        return edtClassName.getText();
    }

    public void setClassName(String className) {
        edtClassName.setText(className);
    }

    private void edtName_keyReleased(KeyEvent e) {
        if(edtName.getText().length() >= 1)
            btnOk.setEnabled(true);
        else
            btnOk.setEnabled(false);        
    }
    private void btnCancel_actionPerformed(ActionEvent e) {
        setApproved(false);
        this.setVisible(false);    
    }
    
    private void btnOk_actionPerformed(ActionEvent e) {
        setApproved(true);
        this.setVisible(false);
    }
    
    public boolean isApproved() {
        return approved;
    }

    protected void setApproved(boolean approved) {
        this.approved = approved;
    }
    
    
    protected boolean approved = false;
    
}
